package com.xiaouyudeguang.common.client;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
import com.xiaouyudeguang.common.cache.RedisCache;
import com.xiaouyudeguang.common.utils.StringUtils;

import java.util.concurrent.TimeUnit;

public class DingTalkClient {

    public static String getToken(String appKey, String appSecret) throws Exception {
        String tokenKey = "dingTalk:token:" + appKey;
        String tokenValue = RedisCache.get(tokenKey);
        if (StringUtils.isBlank(tokenValue)) {
            com.dingtalk.api.DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
            OapiGettokenRequest req = new OapiGettokenRequest();
            req.setAppkey(appKey);
            req.setAppsecret(appSecret);
            OapiGettokenResponse response = client.execute(req);
            if (!response.isSuccess()) {
                throw new Exception("获取token失败." + tokenKey);
            }
            tokenValue = response.getAccessToken();
            RedisCache.set(tokenKey, tokenValue, 7150, TimeUnit.SECONDS);
        }
        return tokenValue;
    }

    public static String loginWeb(String accessKey, String accessSecret, String code) throws Exception {
        OapiSnsGetuserinfoBycodeRequest req = new OapiSnsGetuserinfoBycodeRequest();
        req.setTmpAuthCode(code);
        com.dingtalk.api.DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/sns/getuserinfo_bycode");
        OapiSnsGetuserinfoBycodeResponse response = client.execute(req, accessKey, accessSecret);
        return response.isSuccess() ? response.getUserInfo().getUnionid() : null;
    }

    public static String loginApp(String token, String code) throws Exception {
        OapiUserGetuserinfoRequest req = new OapiUserGetuserinfoRequest();
        req.setCode(code);
        com.dingtalk.api.DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getuserinfo");
        OapiUserGetuserinfoResponse response = client.execute(req, token);
        return response.isSuccess() ? response.getUserid() : null;
    }

    public static String getUserByUnionId(String token, String unionId) throws Exception {
        OapiUserGetUseridByUnionidRequest req = new OapiUserGetUseridByUnionidRequest();
        req.setUnionid(unionId);
        com.dingtalk.api.DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getUseridByUnionid");
        OapiUserGetUseridByUnionidResponse response = client.execute(req, token);
        return response.isSuccess() ? response.getUserid() : null;
    }

    public static OapiUserGetResponse getMobileByUserId(String token, String userId) throws Exception {
        OapiUserGetRequest req = new OapiUserGetRequest();
        req.setUserid(userId);
        com.dingtalk.api.DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get_by_mobile");
        OapiUserGetResponse response = client.execute(req, token);
        return response.isSuccess() ? response : null;
    }
}
